package com.hdax.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hdax.entity.CusOrder;
import com.hdax.service.CusOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/cusOrder")
public class CusOrderController {
    @Autowired
    private CusOrderService cusOrderService;

    @GetMapping("/toOrder")
    public ModelAndView toOrder() {
        return new ModelAndView("order/cus_order");
    }

    @PostMapping("/findCusOrder")
    public IPage<CusOrder> findCusOrder(Integer page, Integer limit) {
        IPage<CusOrder> iPage = new Page<>(page, limit);
        return cusOrderService.findCusOrder(iPage, new HashMap());
    }

    @GetMapping("/toOrderPage")
    public ModelAndView toOrderPage(Integer id) {
        ModelAndView modelAndView = new ModelAndView("order/cus_order_page");
        if (id != null) {
            CusOrder cusOrder = cusOrderService.findCusOrderById(id);
            System.out.println(cusOrder);
            modelAndView.addObject("cusOrder", cusOrder);
        }
        return modelAndView;
    }

    @PostMapping("/savaOrder")
    public String savaOrder(CusOrder cusOrder) {
        Random random = new Random();
        String result = "";
        for (int i = 0; i < 6; i++) {
            result += random.nextInt(10);
        }
        cusOrder.setOrderId("XP" + result);
        cusOrder.setCreateTime(LocalDateTime.now());
        cusOrder.setDepositTime(LocalDateTime.now());
        cusOrderService.save(cusOrder);
        return "success";
    }

    @PostMapping("/updateOrder")
    public String updateOrder(CusOrder cusOrder){
        System.out.println(cusOrder);
        Integer orderState = cusOrder.getOrderState();
        if(orderState == 2){
            cusOrder.setSubsequentTime(LocalDateTime.now());
        }else if(orderState == 3){
            cusOrder.setUpdateTime(LocalDateTime.now());
        }
        cusOrderService.updateById(cusOrder);
        return "success";
    }

    @PostMapping("/findCusOrderCount")
    public Map findCusOrderCount(){
        Map map = new HashMap();
        map.put("month",cusOrderService.findThisMonthCusOrderAddCount());
        map.put("all",cusOrderService.findCusOrderCount());
        return map;
    }

    @PostMapping("/findCusOrderPrice")
    public Map findCusOrderPrice(){
        Map map = new HashMap();
        map.put("month",cusOrderService.findThisMonthCusOrderPrice());
        map.put("all",cusOrderService.findCusOrderPrice());
        return map;
    }

    @PostMapping("/findCusOrderChainCompare")
    public Float findCusOrderChainCompare(){
        return cusOrderService.findCusOrderChainCompare();
    }

    @PostMapping("/findThisYearCusOrderIncre")
    public List findThisYearCustomerIncre(){
        Map thisYearCusOrderIncre = cusOrderService.findThisYearCusOrderIncre();
        ArrayList arrayList = new ArrayList<>(thisYearCusOrderIncre.values());
        return arrayList;
    }


}

